package com.then.libary.view;

import com.then.libary.activity.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MyAlertBox extends Dialog {

	private String InputMessage;

	private ImageView imageView;
	
	public ImageView getImageView() {
		return imageView;
	}



	public void setImageView(ImageView imageView) {
		this.imageView = imageView;
	}



	public String getInputMessage() {
		return InputMessage;
	}

	public void setInputMessage(String inputMessage) {
		InputMessage = inputMessage;
	}

	public MyAlertBox(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public MyAlertBox(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public Builder setPositiveButton(String positiveButtonText,
				DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,
				DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}
        public MyAlertBox createMessage()
        {
        	LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final MyAlertBox myAlertBox = new MyAlertBox(context,
					R.style.Dialog);
			final View layout = inflater.inflate(R.layout.myalert, null);
			myAlertBox.addContentView(layout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			((TextView) layout.findViewById(R.id.title)).setText(title);
			if (negativeButtonText != null) {

				Button negativeButton_btn = (Button) layout
						.findViewById(R.id.negativeButton);
				negativeButton_btn.setText(negativeButtonText);
				if (positiveButtonText != null) {
					((Button) layout.findViewById(R.id.positiveButton))
							.setText(positiveButtonText);
					if (positiveButtonClickListener != null) {
						((Button) layout.findViewById(R.id.positiveButton))
								.setOnClickListener(new View.OnClickListener() {
									public void onClick(View v) {
										
										positiveButtonClickListener
												.onClick(
														myAlertBox,
														DialogInterface.BUTTON_POSITIVE);

									}
								});
					}
				} else {
					// if no confirm button just set the visibility to GONE
					layout.findViewById(R.id.positiveButton).setVisibility(
							View.GONE);
				}
				if (negativeButtonClickListener != null) {
					negativeButton_btn
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub

									negativeButtonClickListener.onClick(
											myAlertBox,
											DialogInterface.BUTTON_NEGATIVE);

								}
							});
				}
			} else {
				layout.findViewById(R.id.negativeButton).setVisibility(
						View.GONE);
			}
			if (message != null) {
				((TextView) layout.findViewById(R.id.alertmessage)).setText(message);
			} else if (contentView != null) {
				((LinearLayout) layout.findViewById(R.id.message))
						.removeAllViews();
				((LinearLayout) layout.findViewById(R.id.message)).addView(
						contentView, new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
			}
			myAlertBox.setContentView(layout);
			return myAlertBox;
        }
		public MyAlertBox create() {

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final MyAlertBox myAlertBox = new MyAlertBox(context,
					R.style.Dialog);
			final View layout = inflater.inflate(R.layout.alertbox, null);
			ImageView imageView = (ImageView) layout.findViewById(R.id.captcha_img);
			myAlertBox.setImageView(imageView);
			myAlertBox.addContentView(layout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			((TextView) layout.findViewById(R.id.title)).setText(title);
			if (negativeButtonText != null) {

				Button negativeButton_btn = (Button) layout
						.findViewById(R.id.negativeButton);
				negativeButton_btn.setText(negativeButtonText);
				if (positiveButtonText != null) {
					((Button) layout.findViewById(R.id.positiveButton))
							.setText(positiveButtonText);
					if (positiveButtonClickListener != null) {
						((Button) layout.findViewById(R.id.positiveButton))
								.setOnClickListener(new View.OnClickListener() {
									public void onClick(View v) {
										EditText editText = (EditText) layout
												.findViewById(R.id.captcha);
										myAlertBox.setInputMessage(editText
												.getText().toString());
										positiveButtonClickListener
												.onClick(
														myAlertBox,
														DialogInterface.BUTTON_POSITIVE);

									}
								});
					}
				} else {
					// if no confirm button just set the visibility to GONE
					layout.findViewById(R.id.positiveButton).setVisibility(
							View.GONE);
				}
				if (negativeButtonClickListener != null) {
					negativeButton_btn
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub

									negativeButtonClickListener.onClick(
											myAlertBox,
											DialogInterface.BUTTON_NEGATIVE);

								}
							});
				}
			} else {
				layout.findViewById(R.id.negativeButton).setVisibility(
						View.GONE);
			}
			if (message != null) {
				((TextView) layout.findViewById(R.id.message)).setText(message);
			} else if (contentView != null) {
				((LinearLayout) layout.findViewById(R.id.message))
						.removeAllViews();
				((LinearLayout) layout.findViewById(R.id.message)).addView(
						contentView, new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
			}
			myAlertBox.setContentView(layout);
			return myAlertBox;
		}
	}

}
